package my.study.c.consul.consumer.app.hello.contoller;

import my.study.c.consul.consumer.app.hello.service.MyFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;

@RestController
public class HelloController {

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    MyFeignClient feignClient;

    @GetMapping("/hello")
    public String hello() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
        URI uri = serviceInstance.getUri();
        String hello = new RestTemplate().getForObject(uri + "/hello", String.class);
        System.out.println(hello);
        return hello;
    }

    @GetMapping("/feign/hello")
    public String hello2() {
        return feignClient.hello();
    }
}
